package com.keensense.vcm.internal;

import static com.keensense.vcm.common.parser.ResponseParsers.queryArchivesResponseParser;
import static com.keensense.vcm.common.parser.ResponseParsers.statisticArchivesResponseParser;
import static com.keensense.vcm.common.utils.CodingUtils.assertParameterNotNull;
import static com.keensense.vcm.internal.VCMResourcePathConstants.PROFILE;
import static com.keensense.vcm.internal.VCMResourcePathConstants.PROFILE_QUERY_FACES;
import static com.keensense.vcm.internal.VCMResourcePathConstants.QUERY_UNFILED_FACES;
import static com.keensense.vcm.internal.VCMResourcePathConstants.manual_create_archives;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.MessageFormat;

import com.keensense.vcm.HttpMethod;
import com.keensense.vcm.common.auth.CredentialsProvider;
import com.keensense.vcm.common.comm.RequestMessage;
import com.keensense.vcm.common.comm.ServiceClient;
import com.keensense.vcm.common.parser.ResponseParsers;
import com.keensense.vcm.model.*;

/**
 * @author lanjian
 * @date 2020/4/29 9:33 上午
 */
public class ProfileOperation extends VCMOperation {

    public ProfileOperation(ServiceClient client, CredentialsProvider credsProvider) {
        super(client, credsProvider);
    }

    /**
     * 根据档案id查看档案详情信息
     */
    public BatchQueryProfileResponse queryProfileById(String id) {
        String resourcePath = PROFILE + "/" + id;
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(endpoint).setMethod(HttpMethod.GET).build();
        request.setResourcePath(resourcePath);
        return doOperation(request, queryArchivesResponseParser, true);
    }

    /**
     * 人员档案详细信息展示，展示动态库过脸信息
     *
     * @param profileQueryFacesRequest
     * @return
     */
    public ProfileQueryFacesResponse profileQueryFaces(ProfileQueryFacesRequest profileQueryFacesRequest) {
        String xml = getXmlFromObject(profileQueryFacesRequest, ProfileQueryFacesRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length).setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(PROFILE_QUERY_FACES);
        return doOperation(request, ResponseParsers.profilePeopleFacesResponseParser, true);
    }

    /**
     * 查询未归档过脸数据
     *
     * @return
     */
    public ProfileQueryFacesResponse queryUnFiledFaces(QueryUnFiledFaceRequest queryUnFiledFaceRequest) {
        String xml = getXmlFromObject(queryUnFiledFaceRequest, QueryUnFiledFaceRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length).setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(QUERY_UNFILED_FACES);
        return doOperation(request, ResponseParsers.profilePeopleFacesResponseParser, true);
    }

    /**
     * <p>
     * 手动添加人员档案
     * </p>
     * <p>
     * 1、添加档案必须指定静态库，档案会同步添加到静态库中。
     * </p>
     * <p>
     * 2、添加档案必须指定已经建档的静态库。
     * </p>
     *
     * @param manualCreateProfileRequest
     * @return
     */
    public ManualCreateProfileResponse manualCreateProfile(ManualCreateProfileRequest manualCreateProfileRequest) {
        String xml = getXmlFromObject(manualCreateProfileRequest, ManualCreateProfileRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length).setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(manual_create_archives);
        return doOperation(request, ResponseParsers.manualCreateArchivesResponseParser, true);
    }

    /**
     * <p>
     * 根据静态库创建建档
     * </p>
     * <p>
     * 1、增加人员档案，支持将静态库迁移到档案库。
     * </p>
     * <p>
     * 2、定时扫描静态库，将该静态库新增人员创建成档案
     * </p>
     *
     * @param addProfileRequest
     */
    public VCMResponse createByLibrary(AddProfileRequest addProfileRequest) {
        String staticLibrary = addProfileRequest.getStaticLibrary();
        assertParameterNotNull(staticLibrary, "staticLibrary");
        String xml = getXmlFromObject(addProfileRequest, AddProfileRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                        .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.CREATE_BY_LIBRARY);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * 更新档案
     *
     * @return
     */
    public VCMResponse updateProfile(UpdateProfileRequest updateProfileRequest) {
        String xml = getXmlFromObject(updateProfileRequest, UpdateProfileRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                        .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        String resourcePath = PROFILE + "/" + updateProfileRequest.getId();
        request.setResourcePath(resourcePath);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * <p>
     * 1、合并人员档案，支持多个档案合并，支持无名档合并，无名档合入有名档。
     * </p>
     *
     * @return
     */
    public VCMResponse mergeProfile(MergeProfileRequest mergeProfileRequest) {
        String xml = getXmlFromObject(mergeProfileRequest, MergeProfileRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                        .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.MERGE_ARCHIVES);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * 将档案从档案库中删除
     *
     * @return
     */
    public VCMResponse delProfileFromLibrary(OperationProfileFromLibraryRequest operationProfileFromLibraryRequest) {
        String xml = getXmlFromObject(operationProfileFromLibraryRequest, OperationProfileFromLibraryRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                        .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        String resourcePath =
                MessageFormat.format(VCMResourcePathConstants.DEL_FROM_LIBRARY, operationProfileFromLibraryRequest.getId());
        request.setResourcePath(resourcePath);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * <p>
     * 将档案加入到档案库
     * </p>
     * <P>
     * 1、支持档案迁移，可以将有名档中的档案迁移到某个档案库中。
     * </P>
     * <p>
     * 2、支持档案迁移，可以将某个档案库中的档案迁移到别的档案库中。
     * </p>
     *
     * @return
     */
    public VCMResponse addProfileToLibrary(OperationProfileFromLibraryRequest operationProfileFromLibraryRequest) {
        String xml = getXmlFromObject(operationProfileFromLibraryRequest, OperationProfileFromLibraryRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                        .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        String resourcePath =
                MessageFormat.format(VCMResourcePathConstants.ADD_TO_LIBRARY, operationProfileFromLibraryRequest.getId());
        request.setResourcePath(resourcePath);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * 批量添加档案到档案库
     *
     * @return
     */
    public VCMResponse batchAddProfileToLibrary(BatchAddProfileToLibraryRequest batchAddProfileToLibraryRequest) {
        String xml = getXmlFromObject(batchAddProfileToLibraryRequest, BatchAddProfileToLibraryRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.BATCH_ADD_PROFILE_TO_LIBRARY);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * <p>
     * 1、输入人员档案id的情况下会忽略其他信息。
     * </p>
     * 2、人员档案检索，以图搜档的情况下会忽略所有结构化信息。
     *
     * @param batchQueryProfileRequest
     * @return
     */
    public BatchQueryProfileResponse batchQueryProfile(BatchQueryProfileRequest batchQueryProfileRequest) {
        String xml = getXmlFromObject(batchQueryProfileRequest, BatchQueryProfileRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length).setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.QUERY_ARCHIVES);
        return doOperation(request, queryArchivesResponseParser, true);
    }

    /**
     * <p>
     * 迁移档案过脸数据
     * </p>
     *
     * @return
     */
    public VCMResponse migrateTrajectory(MigrateTrajectoryRequest migrateTrajectoryRequest) {
        String xml = getXmlFromObject(migrateTrajectoryRequest, MigrateTrajectoryRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                        .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.migrateTrajectory);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * <p>
     * 人员档案删除
     * </p>
     * <p>
     * 1、删除档案后会自动删除静态库人员
     * </p>
     * <p>
     * 2、支持删除无名档案。
     * </p>
     * <p>
     * 3、若删除静态库数据，则档案变更为无名档案。
     * </p>
     *
     * @param id
     * @return
     */
    public VCMResponse deleteProfile(String id) {
        String resourcePath = PROFILE + "/" + id;
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(this.endpoint).setMethod(HttpMethod.DELETE).build();
        request.setResourcePath(resourcePath);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * 删除所有档案
     *
     * @return
     */
    public VCMResponse deleteAllProfile() {
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(this.endpoint).setMethod(HttpMethod.DELETE).build();
        request.setResourcePath(PROFILE);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * 根据图片搜索档案
     *
     * @return
     */
    public BatchQueryProfileResponse queryByPicture(QueryProfileByPictureRequest queryProfileByPictureRequest) {
        String xml = getXmlFromObject(queryProfileByPictureRequest, QueryProfileByPictureRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length)
                        .setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.QUERY_PROFILE_BY_PICTURE);
        return doOperation(request, queryArchivesResponseParser, true);
    }

    /**
     * 人员档案统计接口，查询接口的两个字段互斥，不能同时填写。
     *
     * @return
     */
    public StatisticArchivesResponse statisticProfile() {

        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(this.endpoint).setMethod(HttpMethod.GET).build();
        request.setResourcePath(VCMResourcePathConstants.PROFILE_STATISTICS);
        return doOperation(request, statisticArchivesResponseParser, true);
    }

}
